package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.bill.AdjustOutBill;
import com.canaan.business.domain.vo.AdjustOutBillDetailVo;
import com.canaan.business.service.bill.IAdjustOutBillService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.AdjustOutBillDetailMapper;
import com.canaan.business.domain.bill.AdjustOutBillDetail;
import com.canaan.business.service.bill.IAdjustOutBillDetailService;

/**
 * 调拔出库单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Service
public class AdjustOutBillDetailServiceImpl implements IAdjustOutBillDetailService {
    @Resource
    private AdjustOutBillDetailMapper adjustOutBillDetailMapper;
    @Resource
    private IAdjustOutBillService adjustOutBillService;

    /**
     * 查询调拔出库单明细
     *
     * @param id 调拔出库单明细主键
     * @return 调拔出库单明细
     */
    @Override
    public AdjustOutBillDetail selectAdjustOutBillDetailById(Long id) {
        return adjustOutBillDetailMapper.selectAdjustOutBillDetailById(id);
    }

    @Override
    public List<AdjustOutBillDetailVo> selectAdjustOutBillDetailListPageDatas(AdjustOutBillDetail adjustOutBillDetail) {
        return adjustOutBillDetailMapper.selectAdjustOutBillDetailListPageDatas(adjustOutBillDetail);
    }

    /**
     * 查询调拔出库单明细列表
     *
     * @param adjustOutBillDetail 调拔出库单明细
     * @return 调拔出库单明细
     */
    @Override
    public List<AdjustOutBillDetail> selectAdjustOutBillDetailList(AdjustOutBillDetail adjustOutBillDetail) {
        return adjustOutBillDetailMapper.selectAdjustOutBillDetailList(adjustOutBillDetail);
    }

    /**
     * 新增调拔出库单明细
     *
     * @param adjustOutBillDetail 调拔出库单明细
     * @return 结果
     */
    @Override
    public int insertAdjustOutBillDetail(AdjustOutBillDetail adjustOutBillDetail) {
        if(StringUtils.isBlank(adjustOutBillDetail.getNo())){
            adjustOutBillDetail.setNo(TableNoUtil.getNo(AdjustOutBillDetail.class));
        }
        if(StringUtils.isEmpty(adjustOutBillDetail.getCreateBy())){
            adjustOutBillDetail.setCreateBy(SecurityUtils.getUsername());
        }
        adjustOutBillDetail.setCreateTime(DateUtils.getNowDate());
//        refreshTotalQty(adjustOutBillDetail.getBillNo());
        int i = adjustOutBillDetailMapper.insertAdjustOutBillDetail(adjustOutBillDetail);
        return i;
    }
    private void refreshTotalQty(String billNo){
        AdjustOutBillDetail detail = new AdjustOutBillDetail();
        detail.setBillNo(billNo);
        List<AdjustOutBillDetail> details = this.selectAdjustOutBillDetailList(detail);
        AdjustOutBill bill = adjustOutBillService.selectAdjustOutBillByNo(billNo);
        bill.setTotalQty(details.stream().filter(e->e.getQty()!=null).mapToLong(e -> e.getQty()).sum());
        adjustOutBillService.updateAdjustOutBill(bill);
    }
    /**
     * 批量新增调拔出库单明细
     *
     * @param adjustOutBillDetailList 调拔出库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertAdjustOutBillDetail(List<AdjustOutBillDetail> adjustOutBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustOutBillDetail adjustOutBillDetail : adjustOutBillDetailList){
            if(StringUtils.isBlank(adjustOutBillDetail.getNo())){
                adjustOutBillDetail.setNo(TableNoUtil.getNo(AdjustOutBillDetail.class));
            }
            adjustOutBillDetail.setCreateBy(username);
            adjustOutBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<adjustOutBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustOutBillDetailList.size()) {
                endIndex = adjustOutBillDetailList.size();
            }
            insertNum = insertNum + adjustOutBillDetailMapper.batchInsertAdjustOutBillDetail(adjustOutBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改调拔出库单明细
     *
     * @param adjustOutBillDetail 调拔出库单明细
     * @return 结果
     */
    @Override
    public int updateAdjustOutBillDetail(AdjustOutBillDetail adjustOutBillDetail) {
        adjustOutBillDetail.setUpdateBy(SecurityUtils.getUsername());
        adjustOutBillDetail.setUpdateTime(DateUtils.getNowDate());
        int i = adjustOutBillDetailMapper.updateAdjustOutBillDetail(adjustOutBillDetail);
        refreshTotalQty(adjustOutBillDetail.getBillNo());
        return i;
    }

    /**
     * 批量修改调拔出库单明细
     *
     * @param adjustOutBillDetailList 调拔出库单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateAdjustOutBillDetail(List<AdjustOutBillDetail> adjustOutBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustOutBillDetail adjustOutBillDetail : adjustOutBillDetailList){
            adjustOutBillDetail.setUpdateBy(username);
            adjustOutBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<adjustOutBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustOutBillDetailList.size()) {
                endIndex = adjustOutBillDetailList.size();
            }
            updateNum = updateNum + adjustOutBillDetailMapper.batchUpdateAdjustOutBillDetail(adjustOutBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除调拔出库单明细
     *
     * @param ids 需要删除的调拔出库单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteAdjustOutBillDetailByIds(Long[] ids) {
        int i = adjustOutBillDetailMapper.deleteAdjustOutBillDetailByIds(ids);
        final AdjustOutBillDetail detail = this.selectAdjustOutBillDetailById(ids[0]);
        refreshTotalQty(detail.getBillNo());
        return i;
    }

    /**
     * 删除调拔出库单明细信息
     *
     * @param id 调拔出库单明细主键
     * @return 结果
     */
    @Override
    public int deleteAdjustOutBillDetailById(Long id) {
        return adjustOutBillDetailMapper.deleteAdjustOutBillDetailById(id);
    }

    /**
     * 查询调拔出库单明细
     *
     * @param no 编号
     * @return 调拔出库单明细
     */
    @Override
    public AdjustOutBillDetail selectAdjustOutBillDetailByNo(String no) {
        return adjustOutBillDetailMapper.selectAdjustOutBillDetailByNo(no);
    }

    /**
     * 批量删除调拔出库单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteAdjustOutBillDetailByNos(String[] nos) {
        return adjustOutBillDetailMapper.deleteAdjustOutBillDetailByNos(nos);
    }

    /**
     * 删除调拔出库单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteAdjustOutBillDetailByNo(String no) {
        return adjustOutBillDetailMapper.deleteAdjustOutBillDetailByNo(no);
    }
}
